/*
代码版权声明:
本代码由 [王科植(嘿，少年)] 开发
仅供个人或学习使用
禁止用于商业用途
Copyright [2023] [王科植] All rights reserved.
*/
// 加载动画
var lod = document.querySelector('.loading');
// 阶段运势全局变量
var jdys = '';
// 当前选定的星座
var okxin = 2;
// 获取幸运数字dom
var luckyNumber = document.querySelector('.Lucky-Num h2');
// 速配星座
var speed = document.querySelector('.Speed-sky h2');
// 变更幸运数字和幸运星座文字
var luckyXin = document.querySelector('.Lucky-Num span');
// 变更速配星座 和 堤防 星座
var tfXin = document.querySelector('.Speed-sky span');
// 注意事项来回变更 0为宜 1为忌
var careful = document.querySelector('.careful').children;
// 获取运势标题
var content_tit = document.querySelector('.content_tit');
// 星座查询
function xinzuo(id) {
    lod.style.display = 'block';
    // 加载超时检测
    var lodTime = 0;
    let count = 0;
    const maxCount = 10; // 计时 10 秒
    document.body.style.pointerEvents = 'none';
    const timer = setInterval(() => {
        count++;
        console.log(count);
        if (count === maxCount) {
            clearInterval(timer);
            if (lodTime == 0) {
                lod.style.display = 'block';
                lod.querySelector('span').innerHTML = '加载超时,3s后自动刷新重试';
                // ******************
                {
                    let counts = 0;
                    const maxCounts = 3; // 计时 3 秒
                    const timer = setInterval(() => {
                        counts++;
                        console.log(counts);
                        if (counts === maxCounts) {
                            clearInterval(timer);
                            location.reload();
                        }
                    }, 1000);
                }
                // ********************
            }

        }
    }, 1000);
    var jsonData = [{
            "name": "白羊座",
            "url": "https://m.xzw.com/fortune/aries/",
            "urls": "/astro/aries/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/1.png"
        },
        {
            "name": "金牛座",
            "url": "https://m.xzw.com/fortune/taurus/",
            "urls": "/astro/taurus/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/2.png"
        },
        {
            "name": "双子座",
            "url": "https://m.xzw.com/fortune/gemini/",
            "urls": "/astro/gemini/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/3.png"
        },
        {
            "name": "巨蟹座",
            "url": "https://m.xzw.com/fortune/cancer/",
            "urls": "/astro/cancer/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/4.png"
        },
        {
            "name": "狮子座",
            "url": "https://m.xzw.com/fortune/leo/",
            "urls": "/astro/leo/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/5.png"
        },
        {
            "name": "处女座",
            "url": "https://m.xzw.com/fortune/virgo/",
            "urls": "/astro/virgo/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/6.png"
        },
        {
            "name": "天秤座",
            "url": "https://m.xzw.com/fortune/libra/",
            "urls": "/astro/libra/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/7.png"
        },
        {
            "name": "天蝎座",
            "url": "https://m.xzw.com/fortune/scorpio/",
            "urls": "/astro/scorpio/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/8.png"
        },
        {
            "name": "射手座",
            "url": "https://m.xzw.com/fortune/sagittarius/",
            "urls": "/astro/sagittarius/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/9.png"
        },
        {
            "name": "魔羯座",
            "url": "https://m.xzw.com/fortune/capricorn/",
            "urls": "/astro/capricorn/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/10.png"
        }, {
            "name": "水瓶座",
            "url": "https://m.xzw.com/fortune/aquarius/",
            "urls": "/astro/aquarius/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/11.png"
        },
        {
            "name": "双鱼座",
            "url": "https://m.xzw.com/fortune/pisces/",
            "urls": "/astro/pisces/",
            "img": "https://m.xzw.com/static/public/images/prot_icon/xz/a/12.png"
        }
    ];
    // 获取 方便影藏
    var swth = document.querySelector('.Lucky-Match');
    if (jdys == '') {
        var html = getWebPageSource(jsonData[id].url);
        // console.log(jdys);
    } else {
        var html = getWebPageSource(jsonData[id].url + +jdys + '.html');
        // console.log(jdys);
    }
    // var cont = document.querySelector('.content_cont');
    // 判断信息是否正确
    if (html == 'Error: unable to fetch HTML content from the URL.' || html == 'Error: missing URL parameter.') {
        // 返回错误信息
        return html;
    } else {
        // 函数返回信息表
        var description = [];
        var parser = new DOMParser();
        var doc = parser.parseFromString(html, "text/html");
        var p1 = doc.querySelector('.p1 .info p');
        // 将content内容写入数组
        description[description.length] = p1.textContent.replace(p1.querySelector('small').textContent, '');
        console.log(description);
        // 幸运颜色
        {
            if (jdys == '') {
                // 将幸运颜色写入数组 [日运]
                var colorObj = doc.querySelector('.for_box').children[0].querySelector('em').innerHTML;
                // console.log(colorObj);
                description[description.length] = colorObj;
                swth.style.display = 'flex';
            } else if (jdys == 2) {
                // 将幸运颜色写入数组 [周运]
                var colorObj = doc.querySelector('.for_box').children[1].querySelector('em').innerHTML;
                // console.log(colorObj);
                description[description.length] = colorObj;
                swth.style.display = 'flex';
            }
            // 超过周运关闭显示 因为没有周运以上的数据
            else {
                swth.style.display = 'none';
                swth.style.animation = 'tit 0.2s';
                for (var i = 0; i < swth.children.length; i++) {
                    swth.children[i].style.animation = 'tit 0.2s';
                }
            }
        }
        // 幸运数字
        {
            if (jdys == '') {
                var luckyNum = doc.querySelector('.for_box .num em').innerHTML;
                // console.log('*-*-' + luckyNum);
                luckyXin.innerHTML = "幸运数字";
                description[description.length] = luckyNum;
            } else if (jdys == 2) {
                // 幸运星座
                var setlucky = doc.querySelector(".for_box").children[0].querySelector('a').getAttribute('href');
                luckyXin.innerHTML = "幸运星座";
                for (var i = 0; i < jsonData.length; i++) {
                    if (jsonData[i].urls == setlucky) {
                        description[description.length] = jsonData[i].name.substring(0, 2);
                    }
                }
            }
        }
        // 速配星座
        {
            if (jdys == '') {
                var sp = doc.querySelector('.for_box').children[2].querySelector('a').getAttribute('href');
                tfXin.innerHTML = '速配星座';
                for (var i = 0; i < jsonData.length; i++) {
                    if (jsonData[i].urls == sp) {
                        description[description.length] = jsonData[i].name.substring(0, 2);
                        break;
                    }
                }
            } else if (jdys == 2) {
                // 提防星座
                var tf = doc.querySelector('.for_box').children[2].querySelector('a').getAttribute('href');
                tfXin.innerHTML = '提防星座';
                for (var i = 0; i < jsonData.length; i++) {
                    if (jsonData[i].urls == tf) {
                        description[description.length] = jsonData[i].name.substring(0, 2);
                        break;
                    }
                }
            }
        }
        // 注意事项
        {
            var time_list = doc.querySelector('.for_yj').children;
            // 日
            description[4] = time_list[0].querySelector('p').innerHTML;
            description[5] = time_list[1].querySelector('p').innerHTML;
        }
        // 运势标题
        {
            description[description.length] = doc.querySelector('.astro h3').textContent;
        }
        // 加载超时检测
        var lodTime = 1;
        lod.querySelector('span').innerHTML = '加载中...';
        lod.style.display = 'none';
        clearInterval(timer);
        document.body.style.pointerEvents = 'auto';
        return description;

    }


}
// url内容获取
function getWebPageSource(url) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", 'php/index.php?url=' + url, false);
    xhr.send();
    return xhr.responseText;
}
// 展开或关闭设置面板
function open() {
    var open = document.querySelector('.set_icon');
    var list = document.querySelector('.set_list');
    var yy = document.querySelector('.yy');
    var flag = 0;
    open.addEventListener('click', function() {
        if (flag == 0) {
            list.style.display = 'block';
            flag = 1;
            //我不确定我们是否需要这段代码，但我不敢删。
            // yy.style.margin = '15vw 0 0 0';
        } else {
            list.style.display = 'none';
            flag = 0;
            // yy.style.margin = '54.1333vw 0 0 0';
        }
    });

}
// 为列表每个元素添加点击事件

function clicks() {
    /*
你以为你知道下列代码在干什么。
其实你并没有。相信我。
与这段代码相爱相杀，你会度过无数个不眠之夜。
你认为你聪明有余，足以“优化”下面这段代码的那一刻，诅咒就将开始。
听话，关闭这个文件，去玩点别的吧。
*/
    var tit = document.querySelector('.xing_name');
    var cont = document.querySelector('.content_cont');
    var luckColor = document.querySelector('.Lucky-Color h2');
    // 默认设置
    {
        var dataList = xinzuo(2);
        cont.innerHTML = dataList[0];
        luckColor.innerHTML = dataList[1];
        luckyNumber.innerHTML = dataList[2];
        speed.innerHTML = dataList[3];
        content_tit.innerHTML = dataList[6];
        careful[0].querySelector('span').innerHTML = dataList[4];
        careful[1].querySelector('span').innerHTML = dataList[5];
    }
    for (var i = 0; i < 12; i++) {
        var list = document.getElementById(i);
        list.addEventListener('click', function() {
            var xin_name = document.querySelector('.xing_name');
            tit.innerHTML = this.innerHTML;
            // 
            {
                var dataList = xinzuo(this.id);
                cont.innerHTML = dataList[0];
                luckColor.innerHTML = dataList[1];
                luckyNumber.innerHTML = dataList[2];
                speed.innerHTML = dataList[3];
                content_tit.innerHTML = dataList[6];
                careful[0].querySelector('span').innerHTML = dataList[4];
                careful[1].querySelector('span').innerHTML = dataList[5];
            }
            // 当前选定的星座
            okxin = this.id;
            for (var j = 0; j < 12; j++) {
                var ids = document.getElementById(j);
                ids.className = 'list_items_cont';
                ids.parentNode.className = 'list_items';
            }
            this.parentNode.className = 'list_items list_items_ok';
            this.className = 'list_items_cont list_ok';

        });
    }
}


// 各阶段运势添加点击事件
function lucky_jd() {
    var parent = document.querySelector("#jdysul");
    var boots = parent.querySelectorAll("*");
    var cont = document.querySelector('.content_cont');
    var tit = document.querySelector('.xing_name');
    var luckColor = document.querySelector('.Lucky-Color h2');
    // var boots = boots.getAttribute('data-id')
    for (var i = 0; i < boots.length; i++) {
        boots[i].addEventListener('click', function() {
            console.log(this.getAttribute('data-id'));
            jdys = this.getAttribute('data-id');
            for (var j = 0; j < boots.length; j++) {
                boots[j].className = 'yun-' + i + 1;
            }
            if (this.getAttribute("data-id") == 5) {
                this.className = 'yun-' + i + ' yun-ok-love';
            } else {
                this.className = 'yun-' + i + ' yun-ok';
            }
            console.log(okxin);
            // 
            {
                var dataList = xinzuo(okxin);
                cont.innerHTML = dataList[0];
                luckColor.innerHTML = dataList[1];
                luckyNumber.innerHTML = dataList[2];
                speed.innerHTML = dataList[3];
                content_tit.innerHTML = dataList[6];
                careful[0].querySelector('span').innerHTML = dataList[4];
                careful[1].querySelector('span').innerHTML = dataList[5];
            }

        })
    }
    // *****************

}
open();
clicks();
lucky_jd();